package com.example.bomc.service.userService.impl;

import com.example.bomc.dao.UserDAO;
import com.example.bomc.dto.loginDto;
import com.example.bomc.model.User;
import com.example.bomc.service.userService.UserService;
import com.example.bomc.common.utils.BOMCUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    /**
     * 注册
     *
     * @param username   用户名
     * @param password   密码
     * @param isCapacity 用户身份标记
     * @return
     */
    @Override
    public Map<String, Object> register(String username, String password, Integer isCapacity) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isBlank(username)) {
            map.put("msgname", "用户名不能为空");
            return map;
        }

        if (StringUtils.isBlank(password)) {
            map.put("msgpwd", "密码不能为空");
            return map;
        }

        User user = userDAO.selectByName(username);

        if (user != null) {
            map.put("msgname", "用户名已经被注册");
            return map;
        }

        user = new User();
        user.setUsername(username);
        user.setSalt(UUID.randomUUID().toString().substring(0, 5));
        user.setPassword(BOMCUtil.MD5(password + user.getSalt()));
        user.setIsCapacity(isCapacity);
        user.setIsExamined(0);//默认设置为0,代表未审核
        userDAO.addUser(user);
        map.put("success", username);

        return map;
    }

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @Override
    public loginDto login(String username, String password) {
        loginDto ll = new loginDto();
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isBlank(username)) {
            map.put("msgname", "用户名不能为空");
            ll.setStatus(1);
            ll.setMap(map);
            return ll;
        }

        if (StringUtils.isBlank(password)) {
            map.put("msgpwd", "密码不能为空");
            ll.setStatus(1);
            ll.setMap(map);
            return ll;
        }

        User user = userDAO.selectByName(username);

        if (user == null) {
            map.put("msgname", "用户不存在");
            ll.setStatus(1);
            ll.setMap(map);
            return ll;
        }

        if (!BOMCUtil.MD5(password + user.getSalt()).equals(user.getPassword())) {
            map.put("msgpwd", "密码不正确");
            ll.setStatus(1);
            ll.setMap(map);
            return ll;
        }

        if (user.getIsExamined() == 0) {
            map.put("msg", "未经过审核，请联系管理员进行审核");
            ll.setStatus(1);
            ll.setMap(map);
            return ll;
        }
        map.put("msg", "登录成功");
        ll.setStatus(0);
        ll.setMap(map);
        return ll;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User byName = userDAO.findByName(username);
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(byName.getRoleCode());
        grantedAuthorities.add(grantedAuthority);
        return new org.springframework.security.core.userdetails.User(byName.getUsername(), byName.getPassword(), grantedAuthorities);
    }
}
